/**
 * 
 */
package com.laozhao.deliveryManager.zgui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * @author laozhao
 *
 */
public class CloseableTabbedPane extends JTabbedPane{
	
	private Component closeComponent ;
//	private JPanel firstPanel ;
//	private String firstTitle ;
	private boolean isClose = false;
	
	public void addCloseableTab(String title, Component component) {
		final Component comp = component;
		JPanel tab = new JPanel();
		tab.setOpaque(false);
		
		JLabel tabLabel = new JLabel(title);
		ImageIcon closeXIcon = new ImageIcon("D:/close.gif");
//		tabLabel.setToolTipText(title);
		
		Dimension closeButtonSize = new Dimension(
		        closeXIcon.getIconWidth()+2,closeXIcon.getIconHeight()+2);

		JButton closeButton = new JButton(closeXIcon);
		closeButton.setPreferredSize(closeButtonSize);
		closeButton.setToolTipText("关闭");
		closeButton.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
            	   //原方案有未知缺陷，故修改为，仅剩下一个时，不能关闭！
                      if(getSelectedComponent() == comp && getTabCount() > 1){
//                 		 System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&^close listener star  ^^^^^^^^^^^^^^^^^^^^^^^^");
//                    	  //如果移除完毕，则添加firstPanel firstTitle
//                          if(getTabCount() == 1){
//                        	  JPanel component = new JPanel();
//                        	  String title = "";
//                        	  if(firstPanel != null){
//                        		  component.add(firstPanel);
//                        	  }
//                        	  if(firstTitle != null){
//                        		  title = firstTitle;
//                        	  }
//                        	  addCloseableTab(title,component);
//                          }
                          //移除组件
                          closeComponent = comp;
                          isClose = true;
                          remove(comp);
//                 		 System.out.println("==========================close  listener end ^^^^^^^^^^^^^^^^^^^^^^^^");
                      }
                 }
              });
		
		tab.add(tabLabel,BorderLayout.WEST);
	    tab.add(closeButton,BorderLayout.EAST);
		super.addTab(null,comp);
		super.setTabComponentAt(super.getTabCount()-1, tab);
		setSelectedComponent(comp);
		setSelectedIndex(super.getTabCount()-1);
		
    }
	 protected void fireStateChanged() {
//		 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^father listener ^^^^^^^^^^^^^^^^^^^^^^^^");
		 if(!isClose){
			 closeComponent = null;
		 }else{
			 isClose = false;
		 }
	     super.fireStateChanged();
	 }  
	
	public Component getCloseComponent() {
		return closeComponent;
	}

//	public JPanel getFirstPanel() {
//		return firstPanel;
//	}
//
//	public void setFirstPanel(JPanel firstPanel) {
//		this.firstPanel = firstPanel;
//	}
//
//	public String getFirstTitle() {
//		return firstTitle;
//	}
//
//	public void setFirstTitle(String firstTitle) {
//		this.firstTitle = firstTitle;
//	}
	
}
